/**
 校验搜索表单
 */

function search(form) {
    if ($(form.name).val() == "null") {
        return false;
    }
    return true;
}

// // 删除单个元素
function deleteSupplier(id) {
    swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的，我要删除！",
            cancelButtonText: "让我再考虑一下…",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function (isConfirm) {
            if (isConfirm) {
                sendDeleteRequest(id),
                    swal("删除成功！", "您已经永久删除了这条信息。", "success");
            } else {
                swal("已取消", "您取消了删除操作！", "error");
            }
        });
}
//发送删除请求
function sendDeleteRequest(id) {
    $.post(getRootPath()+"/supplier_list/delete/" + id, "", function (json) {
        if (json.code === 200) {
            Tips.showSuccess(json.msg);
            //定时刷新页面
            setTimeout(function () {
                window.location.reload();
            }, 2000);
        } else {
            Tips.showError(json.msg)
        }
    }, "json");
}
//添加
function addSupplier(form) {
    $.ajax({
        type:"post",
        "url": getRootPath()+"/supplier_list/add",
        "data":  "suppliername=" + $(form.suppliername).val() + "&supplierphone="+$(form.supplierphone).val()+ "&supplieraddress="+$(form.supplieraddress).val(),
        "async": false,
        "dataType": "json",
        "success": function (result) {
            if (result.code == 200) {
                swal({
                    title: "添加成功！",
                    type: "success"
                });
                setTimeout(function () {
                    window.location.reload();
                }, 2000);
            } else {
                swal("添加失败",  "error");
            }
        }
    });
    return false;
}

function updateSupplier(form) {
    $.ajax({
        type:"post",
        "url": getRootPath()+"/supplier_list/update",
        "data":"supplierName2=" + $(form.supplierName2).val()+"&supplierPhone2="+$(form.supplierPhone2).val()+"&supplierAddress2="+$(form.supplierAddress2).val()+ "&id="+$(form.id).val(),
        "async": false,
        "dataType": "json",
        "success": function (result) {
            if (result.code == 200) {
                setTimeout(function () {
                    window.location.reload();
                }, 2000);
            } else {
                error.innerHTML = json.msg;
            }
        }
    });
    return false;
}
//更新回显
function toupdateSupplier(id) {
    $.ajax({
        type:"post",
        "url": getRootPath()+"/supplier_list/find/"+id,
        "data":  "",
        "async": false,
        "dataType": "json",
        "success": function (result) {
            if (result.code == 200) {
                $("#suppliername2").val(result.data.ename);
                $("#supplierphone2").val(result.data.ephone);
                $("#supplieraddress2").val(result.data.eaddress);
                $("#id").val(result.data.id);
            } else {
                error.innerHTML = json.msg;
            }
        }
    });
}